﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EPPLib.Managers;
using EPPLib.Data;
using EPPLib.Messages;
using MvcMembership;
using System.Web.Security;

namespace EPPWebMVC.Controllers
{
    public class BaseController : Controller
    {
        #region Members
        private ServiceManager _serviceManager;
        private DataManager _dataManager;
        private string _currentAccount;
        private string _currentPassword;
        private string _currentUser;
        private string _currentTargetUrl;
        private string _currentTargetDeletedUrl;
        private Guid _currentUserID;
        #endregion

        #region Properties
        public ServiceManager Service { get { return _serviceManager; } }
        public DataManager Data { get { return _dataManager; } }
        public string CurrentAccount { get { return _currentAccount; } }
        public string CurrentPassword { get { return _currentPassword; } }
        public string CurrentUser { get { return _currentUser; } }
        public string CurrentTargetUrl { get { return _currentTargetUrl; } }
        public string CurrentTargetDeletedUrl { get { return _currentTargetDeletedUrl; } }
        public Guid CurrentUserID { get { return _currentUserID; } }
        #endregion

        #region Constructor(s)
        public BaseController()
        {
            _serviceManager = new ServiceManager();
            _dataManager = new DataManager();
            _currentAccount = string.Empty;
            _currentPassword = string.Empty;
            _currentUser = string.Empty;

            MembershipUser user = Membership.GetUser();
            if (user != null)
            {
                Guid.TryParse(user.ProviderUserKey.ToString(), out _currentUserID);
                _serviceManager.CurrentUserID = _currentUserID;

                _currentUser = user.UserName;
                ConfigurationDataObject config = _dataManager.ReadConfiguration(_currentUserID);
                _currentAccount = config.Account;
                _currentPassword = config.Password;
                _currentTargetUrl = config.TargetUrl;
                _currentTargetDeletedUrl = config.TargetDeletedUrl;
            }

        }
        #endregion

        #region Methods
        protected void SetBaseViewData()
        {
            ViewData["__CURRENT_ACCOUNT"] = _currentAccount;
            ViewData["__CURRENT_TARGET_CurrentTargetUrl"] = _currentTargetUrl;
            ViewData["__CURRENT_TARGET_DELETED_CurrentTargetUrl"] = _currentTargetDeletedUrl;
            ViewData["__CURRENT_SESSION"] = _serviceManager.Session;
            ViewData["__CURRENT_IS_AUTHENTICATED"] = _serviceManager.IsAuthenticated;
        }

        protected void SetBaseViewData(BaseResponseMessage responseMessage)
        {
            SetBaseViewData();
            ViewData["__RESPONSE_MESSAGE"] = responseMessage;
        }

        protected void SetBaseViewData(HelloResponseMessage responseMessage)
        {
            SetBaseViewData();
            ViewData["__RESPONSE_MESSAGE"] = responseMessage;
        }

        protected void SetBaseViewData(string key, BaseResponseMessage responseMessage)
        {
            SetBaseViewData();
            ViewData[key] = responseMessage;
        }
        #endregion
    }
}